package com.zaicheng.cloud.remotecall;/*
 @author zaicheng
 @version 1.0
 @date 2022-04-19
 @email zzc15847256473@163.com
 */

import java.io.*;
import java.lang.reflect.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.util.*;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

public class Util {

    public static final String CONTENT_LENGTH = "Content-Length";
    public static final String CONTENT_ENCODING = "Content-Encoding";
//    public static final String RETRY_AFTER = "Retry-After";
    public static final String ENCODING_GZIP = "gzip";
    public static final String ENCODING_DEFLATE = "deflate";
    public static final Charset UTF_8 = Charset.forName("UTF-8");
    public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
    private static final int BUF_SIZE = 2048;
    //public static final Type MAP_STRING_WILDCARD = new ParameterizedTypeImpl((Type)null, Map.class, new Type[]{String.class, new WildcardTypeImpl(new Type[]{Object.class}, new Type[0])});
    private static final Map<Class<?>, Supplier<Object>> EMPTIES;

    private Util() {
    }

    public static void checkArgument(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) {
        if (!expression) {
            throw new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs));
        }
    }

    public static <T> T checkNotNull(T reference, String errorMessageTemplate, Object... errorMessageArgs) {
        if (reference == null) {
            throw new NullPointerException(String.format(errorMessageTemplate, errorMessageArgs));
        } else {
            return reference;
        }
    }

    public static void checkState(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) {
        if (!expression) {
            throw new IllegalStateException(String.format(errorMessageTemplate, errorMessageArgs));
        }
    }

    public static boolean isDefault(Method method) {
//        int SYNTHETIC = true;
        return (method.getModifiers() & 5129) == 1 && method.getDeclaringClass().isInterface();
    }

    public static String emptyToNull(String string) {
        return string != null && !string.isEmpty() ? string : null;
    }

    public static <T> T[] removeValues(T[] values, Predicate<T> shouldRemove, Class<T> type) {
        Collection<T> collection = new ArrayList(values.length);
        Object[] array = values;
        int var5 = values.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            T value = (T) array[var6];
            if (shouldRemove.negate().test(value)) {
                collection.add(value);
            }
        }

        array = (Object[])((Object[]) Array.newInstance(type, collection.size()));
        return (T[]) collection.toArray(array);
    }

    public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) {
        Object collection;
        if (iterable instanceof Collection) {
            collection = (Collection)iterable;
        } else {
            collection = new ArrayList();
            Iterator var3 = iterable.iterator();

            while(var3.hasNext()) {
                T element = (T) var3.next();
                ((Collection)collection).add(element);
            }
        }

        T[] array = (T[]) Array.newInstance(type, ((Collection)collection).size());
        return (T[]) ((Collection)collection).toArray(array);
    }

    public static <T> Collection<T> valuesOrEmpty(Map<String, Collection<T>> map, String key) {
        Collection<T> values = (Collection)map.get(key);
        return (Collection)(values != null ? values : Collections.emptyList());
    }

    public static void ensureClosed(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException var2) {
            }
        }

    }

//    public static Type resolveLastTypeParameter(Type genericContext, Class<?> supertype) throws IllegalStateException {
//        Type resolvedSuperType = Types.getSupertype(genericContext, Types.getRawType(genericContext), supertype);
//        checkState(resolvedSuperType instanceof ParameterizedType, "could not resolve %s into a parameterized type %s", genericContext, supertype);
//        Type[] types = ((ParameterizedType)ParameterizedType.class.cast(resolvedSuperType)).getActualTypeArguments();
//
//        for(int i = 0; i < types.length; ++i) {
//            Type type = types[i];
//            if (type instanceof WildcardType) {
//                types[i] = ((WildcardType)type).getUpperBounds()[0];
//            }
//        }
//
//        return types[types.length - 1];
//    }

//    public static Object emptyValueOf(Type type) {
//        return ((Supplier)EMPTIES.getOrDefault(Types.getRawType(type), () -> {
//            return null;
//        })).get();
//    }

    public static String toString(Reader reader) throws IOException {
        if (reader == null) {
            return null;
        } else {
            try {
                StringBuilder to = new StringBuilder();
                CharBuffer charBuf = CharBuffer.allocate(2048);
                CharBuffer buf = charBuf;

                while(reader.read(charBuf) != -1) {
                    buf.flip();
                    to.append(charBuf);
                    buf.clear();
                }

                String var4 = to.toString();
                return var4;
            } finally {
                ensureClosed(reader);
            }
        }
    }

    public static byte[] toByteArray(InputStream in) throws IOException {
        checkNotNull(in, "in");

        byte[] var2;
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            copy(in, out);
            var2 = out.toByteArray();
        } finally {
            ensureClosed(in);
        }

        return var2;
    }

    private static long copy(InputStream from, OutputStream to) throws IOException {
        checkNotNull(from, "from");
        checkNotNull(to, "to");
        byte[] buf = new byte[2048];
        long total = 0L;

        while(true) {
            int r = from.read(buf);
            if (r == -1) {
                return total;
            }

            to.write(buf, 0, r);
            total += (long)r;
        }
    }

    public static String decodeOrDefault(byte[] data, Charset charset, String defaultValue) {
        if (data == null) {
            return defaultValue;
        } else {
            checkNotNull(charset, "charset");

            try {
                return charset.newDecoder().decode(ByteBuffer.wrap(data)).toString();
            } catch (CharacterCodingException var4) {
                return defaultValue;
            }
        }
    }

    public static boolean isNotBlank(String value) {
        return value != null && !value.isEmpty();
    }

    public static boolean isBlank(String value) {
        return value == null || value.isEmpty();
    }

    static {
        Map<Class<?>, Supplier<Object>> empties = new LinkedHashMap();
        empties.put(Boolean.TYPE, () -> {
            return false;
        });
        empties.put(Boolean.class, () -> {
            return false;
        });
        empties.put(byte[].class, () -> {
            return new byte[0];
        });
        empties.put(Collection.class, Collections::emptyList);
        empties.put(Iterator.class, Collections::emptyIterator);
        empties.put(List.class, Collections::emptyList);
        empties.put(Map.class, Collections::emptyMap);
        empties.put(Set.class, Collections::emptySet);
        empties.put(Optional.class, Optional::empty);
        empties.put(Stream.class, Stream::empty);
        EMPTIES = Collections.unmodifiableMap(empties);
    }

}
